
package com.bootdo.alipay.controller;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayMarketingCardOpenRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipayMarketingCardOpenResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.bootdo.alipay.service.AlipayService;
import com.bootdo.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/alipay/card")
public class AlipayCardController {
	@Autowired
	AlipayService alipayService;

	@GetMapping()
	String blog() {
		return "blog/index/main";
	}

	@GetMapping("/auth")
	public String auth(String auth_code) {
		System.out.println("auth_code: "+auth_code);
		AlipayClient alipayClient=alipayService.getClient();
		AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
		request.setCode(auth_code);
		request.setGrantType("authorization_code");
		try {
			AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
			ShiroUtils.setSessionAccessToken("accessToken",oauthTokenResponse.getAccessToken());
			String userid=oauthTokenResponse.getUserId();
			String accessToken=oauthTokenResponse.getAccessToken();
			System.out.println(userid);
			System.out.println(accessToken);
			openCard(userid,accessToken);
		} catch (AlipayApiException e) {
			//处理异常
			e.printStackTrace();
		}
		return "blog/index/main";
	}

	public void openCard(String userid,String accessToken){
		AlipayMarketingCardOpenRequest request = new AlipayMarketingCardOpenRequest();
		request.setBizContent("{" +
				"\"out_serial_no\":\"201606270000001\"," +
				"\"card_template_id\":\"20171027000000000548490000300073\"," +
				"\"card_user_info\":{" +
				"\"user_uni_id\":\""+userid+"\"," +
				"\"user_uni_id_type\":\"UID\"" +
				"    }," +
				"\"card_ext_info\":{" +
				"\"biz_card_no\":\"000001\"," +
				"\"external_card_no\":\"EXT0001\"," +
				"\"open_date\":\"2014-02-20 21:20:46\"," +
				"\"valid_date\":\"2020-02-20 21:20:46\"," +
				"\"level\":\"VIP1\"," +
				"\"point\":\"88\"," +
				"\"balance\":\"124.89\"," +
				"\"mdcode_info\":{" +
				"\"code_status\":\"SUCCESS\"," +
				"\"code_value\":\"1KFCDY0002\"," +
				"\"expire_time\":\"2017-06-09 16:25:53\"," +
				"\"time_stamp\":1496996459" +
				"      }," +
				"        \"front_text_list\":[{" +
				"          \"label\":\"专业\"," +
				"\"value\":\"金融贸易\"" +
				"          }]," +
				"\"front_image_id\":\"9fxnkgt0QFmqKAl5V2BqxQAAACMAAQED\"" +
				"    }," +
				"\"member_ext_info\":{" +
				"\"name\":\"李洋\"," +
				"\"gende\":\"MALE\"," +
				"\"birth\":\"2016-06-27\"," +
				"\"cell\":\"13000000000\"" +
				"    }," +
				"\"open_card_channel\":\"20161534000000000008863\"," +
				"\"open_card_channel_id\":\"2088123123123123\"" +
				"  }");
		AlipayMarketingCardOpenResponse response = null;
		try {
			response = alipayService.getClient().execute(request,accessToken);
			System.out.println(response.getBody());
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		if(response.isSuccess()){
			System.out.println("调用成功");
			System.out.println(response.getBody());
		} else {
			System.out.println("调用失败");
		}
	}
}